package com.tianyalei.wolf.sample.activity.wxcircle.imagedetail;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tianyalei.wolf.sample.R;
import com.tianyalei.wolf.sample.activity.wxcircle.widget.dialog.SavePicDialog;
import com.tianyalei.wolf.sample.imageload.ImageLoadProxy;

import java.util.List;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * @author:Jack Tony
 * @tips :viewpager的适配器
 * @date :2014-11-12
 */
public class ViewPagerAdapter extends PagerAdapter {

    private List<String> sDrawables;
    private Context mContext;
    private ZoomTutorial mZoomTutorial;
    private LayoutInflater inflater;

    public ViewPagerAdapter(Context context, List<String> imgIds, ZoomTutorial zoomTutorial) {
        this.sDrawables = imgIds;
        this.mContext = context;
        this.mZoomTutorial = zoomTutorial;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return sDrawables.size();
    }

    @Override
    public View instantiateItem(ViewGroup container, final int position) {
        View view = inflater.inflate(R.layout.item_pager_image, container, false);
        final String url = sDrawables.get(position);
        if (view != null) {
            final PhotoView imageView = (PhotoView) view.findViewById(R.id.one_image_detail);
            ImageLoadProxy.displayNormalImage(mContext, url, imageView);
            imageView.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
                @Override
                public void onViewTap(View view, float x, float y) {
                    mZoomTutorial.closeZoomAnim(position);
                }
            });
            imageView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    SavePicDialog dialog = new SavePicDialog(mContext, url);
                    dialog.show();
                    return false;
                }
            });
            container.addView(view, ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.MATCH_PARENT);
        }

        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}
